//
//  AppDelegate.swift
//  swift_weibo
//
//  Created by ccl on 16/2/16.
//  Copyright © 2016年 LittleBoy. All rights reserved.
//

import UIKit
import AFNetworking
var shareUserAccount = CCUserAccountToken.loadAccount() ;

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // 设置网络工作指示器
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true;
        // 设置网络缓存 memoryCapacity 内存缓存 diskCapacity 硬盘缓存
        let cacheUrl = NSURLCache(memoryCapacity: 1024 * 1024 * 4, diskCapacity: 10 * 1024 * 1024, diskPath: nil );
        NSURLCache.setSharedURLCache(cacheUrl);
        
        
     // 设置 导航栏bar 的前景色
        let navBar = UINavigationBar.appearance();
        navBar.tintColor = UIColor.orangeColor();
     // 设置 tabbar 的前景色
        let barBar = UITabBar.appearance();
        barBar.tintColor = UIColor.orangeColor();
        
        
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.rootViewController = swichRootVC();
        self.window?.makeKeyAndVisible()
        
        checkVersion();
        
        return true
    }
    
    func swichRootVC()->UIViewController{
    
        if shareUserAccount != nil{
          // 登录了
            return checkVersion();
        }
        // 没有登录
        return myTabBarVC();
    }
    
    func checkVersion()-> UIViewController{
      // 取出当前程序的 version
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleVersion"] as! String;
      // 取出记录的   version
        let systemVersion = NSUserDefaults.standardUserDefaults().objectForKey("systemVersion") as? String;
        if let temp = systemVersion{
            let currentVer = Double(currentVersion);
            let systeyVer = Double(temp);
            // 比较
            if currentVer == systeyVer{
              // 当前版本 等于系统
                return CCWelcomeVC();
            }
        }
        // 将当前版本,保存
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "systemVersion");
        
        // 第一次使用, 和 当前版本 > 系统版本, 都要 进入新特性页;
        return CCNewFeatureCollectionVC();
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

